import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {
        Runnable t = new Runnable() {

            public void run() {
                try {
                    UDPPLUS s = new UDPPLUS(10000);
                    String ff = null;
                    try {
                        HashMap<Integer, byte[]> lista= new HashMap<Integer, byte[]>();
                        while(!s.ligado()) Thread.sleep(1);
                        //FileOutputStream file = new FileOutputStream("C:\\Intel\\temp.out");
                        FileOutputStream file = new FileOutputStream("C:\\teste2.txt");
                        while(s.ligado()){
                            System.out.println("INICIO da thread escreve");
                            byte [] a = s.receive();
                            if(a!=null){
                                int valor = (byte) a[0];
                                
                                byte b[] = new byte[a.length-1];
                                for(int i=1;i<a.length;i++)
                                    b[i-1] = a[i];

                                file.write(b);
                                //lista.put(valor, b);

                                //ff = new String(a);
                               //System.out.println("-----Resultado"+valor);
                            }
                        }

                        s.info();
                        System.out.println("FIM da thread escreve");
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        };
        Thread aa = new Thread(t);
        aa.start();

        UDPPLUS c = new UDPPLUS();
        c.connect("localhost", 10000);
        //InputStream is = new FileInputStream("C:\\Intel\\teste.txt");
        InputStream is = new FileInputStream("C:\\teste.txt");
        int tam=30000;
        int off=1;
        //System.out.println(gg);
        byte bb[] = new byte[tam];
        int i=0;
        //is.re
        int n;
        while( (n=is.read(bb, off, tam-off))>=0){
            bb[0] = (byte) i;
            if(n== tam-off)
            {
                c.send(bb);
            }
            else{
                byte [] cc = new byte[n+off];
                for(int j=0;j<n+off;j++) cc[j] = bb[j];
                c.send(cc);
            }

            String teste = new String(bb);
            //System.out.println("*#*#*#*#*#:" + teste + " **** " + bb[0]);
            
            bb = new byte[tam];
            i++;
        }


        byte []temp = new byte[3];
        temp[0] = (byte) 207;
        temp[1] = (byte) 0;
        temp[2] = (byte) 2;
        
        c.send(temp);

        c.info();
       
        c.close();
    }
/*bla*/
}
